<!--
 * @Description: 
 * @Author: zhihong deng
 * @Date: 2021-07-05 23:01:10
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-07-10 22:23:15
-->
<template>
  <div class="com-container">
    <div class="com-chart" ref="stock_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  data(){
    return{
      chartInstance: '',
      allData: '',
      currentIndex: 0,//当前显示的数据
      timerId: null,//定时器的标识
    }
  },
  computed: {
      ...mapState(['theme'])
    },
    watch: {
      theme () {
        // 第一步：销毁当前图表
        this.chartInstance.dispose()
        // 第二步：重新以最新的主题初始化图表对象
        this.initChart()
        // 第三步：完成屏幕的适配
        this.screenAdapter()
        // 第四步：更新图表的展示
        this.updateChart()
      }
    },
  methods: {
    initChart(){
      this.chartInstance = this.$echarts.init(this.$refs.stock_ref,this.theme);
      const initOption = {
        title: {
          text: '▎库存和销量分析',
          top: 20,
          left: 20,
        },
      };
      this.chartInstance.setOption(initOption)
      this.chartInstance.on('mouseover',() => {
        clearInterval(this.timerId)
      })
      this.chartInstance.on('mouseout',() => {
        this.startInterval()
      })
    },
    getData(ret){
      // const {data: ret} = await this.$http.get('stock');
      this.allData = ret;
      console.log('所有数据：',this.allData);
      this.updataChart();
      this.startInterval()//获取完数据后启动定时器
    },
    updataChart(){
      const centerArr = [
        ['18%','40%'],
        ['50%','40%'],
        ['82%','40%'],
        ['34%','75%'],
        ['66%','75%'],
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = (this.currentIndex + 1) * 5
      const showData = this.allData.slice(start,end)//截取前五组数据
      //处理数据
      const seriesArr = showData.map((item,index) => {
        return{
          type: 'pie',
          // radius: [110,100],//[外环，内环]
          hoverAnimation: false,//关闭鼠标移入到饼图时的动画效果
          center: centerArr[index],//设置各个中心标签
          labelLine: {
            show: false,//隐藏指示线
          },
          label: {
            position: 'center',
            color: colorArr[index][0],
          },
          data: [
            {
              name: item.name + '\n\n' + item.sales,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0,1,0,0,[
                  {
                    offset: 0,
                    color: colorArr[index][0],
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1],
                  }
                ]),
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843',
              },
            }
          ],
        }
      })
      const dataOption = {
        series: seriesArr,
      };
     this.chartInstance.setOption(dataOption)
    },
    screenAdapter(){
      const titleFontSize = this.$refs.stock_ref.offsetWidth / 100 * 3.6;//根据图表容器stock_ref设置自适应
      const innerRadius = titleFontSize * 2.8;//内圆半径
      const outterRadius = innerRadius * 1.125;//外圆半径
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          },
          {
            type: 'pie',
            radius: [outterRadius, innerRadius],
            label: {
              fontSize: titleFontSize / 2
            }
          }
        ]
      }
      this.chartInstance.setOption(adapterOption)
      this.chartInstance.resize()
    },
    startInterval(){
      if(this.timerId){
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.currentIndex++
        if(this.currentIndex > 1){
          this.currentIndex = 0
        }
        this.updataChart()//在更改完currentIndex之后，需要更新界面
      },5000)
    },
  },
  created () {
    this.$socket.registerCallBack('stockData',this.getData)
  },
  mounted(){
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'stockData',
      chartName: 'stock',
      value: ''
    })
    window.addEventListener('resize',this.screenAdapter)
    this.screenAdapter()
  },
  destroyed(){
    window.removeEventListener('resize',this.screenAdapter)
    clearInterval(this.timerId)
    this.$socket.unRegisterCallBack('stockData')
  }
}
</script>

<style>

</style>